package com.jackrain.nea.ad.mapper;

import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author nathan
 */
@Mapper
public interface AduserattrMapper {
    /***
     * 根据id查询此记录是否存在
     * @param id
     * @return
     */
    @Select("SELECT COUNT(1) FROM AD_USER_ATTR WHERE ID = #{id}")
    Integer queryuserattrById(long id);

    /**
     * 修改情况判断查询是否有重复名称
     * @param id
     * @param eName
     * @return
     */
    @Select("SELECT COUNT(1) FROM AD_USER_ATTR WHERE ID!=#{arg0} AND NAME = #{arg1}")
    Integer queryuserattrByENameForUp(Long id, String eName);


    /***
     * 根据ename查询此记录是否存在
     * @param eName
     * @return
     */
    @Select("SELECT COUNT(1) FROM AD_USER_ATTR WHERE NAME = #{eName}")
    Integer queryuserattrByEName(String eName);

    /**
     * 修改用户环境变量
     * @param jo
     * @return
     */
    @UpdateProvider(type = UserattrSql.class,method = "userattrUppdate")
    Integer userattrUppdate(JSONObject jo);

    /**
     * 新增用户环境变量
     * @param jo
     * @return
     */
    @InsertProvider(type = UserattrSql.class,method = "userattrSave")
    Integer userattrAdd(JSONObject jo);

    /***
     * 根据id删除用户环境变量
     * @param id
     * @return
     */
    @Delete("DELETE FROM AD_USER_ATTR WHERE ID = #{id}")
    Integer deleteUserattrById(long id);

    class UserattrSql{
        public String userattrUppdate(JSONObject jo){
            return new SQL(){
                {
                    UPDATE("AD_USER_ATTR");
                    for (String key:jo.keySet()){
                        if (!"ID".equals(key)){
                            SET(key+"="+"#{"+key+"}");
                        }
                    }
                    WHERE("ID = #{ID}");
                }
            }.toString();
        }

        public String userattrSave(JSONObject jo){
            return new SQL(){
                {
                    INSERT_INTO("AD_USER_ATTR");
                    for (String key:jo.keySet()){
                        VALUES(key,"#{"+key+"}");
                    }
                }
            }.toString();
        }
    }

}
